
package ManagedBeans;

import Utilitaires.PropertiesReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.ejb.Remove;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

/**
 *
 * @author SuceurdeChien
 */
@ManagedBean
@SessionScoped
public class NavigationBean
{
    private String login;
    private String password;
    private boolean logged;
    private Properties properties;
    
    public NavigationBean() 
    {
        this.login = "";
        this.password = "";
        this.properties = PropertiesReader.getProperties();
    }
    
    // Méthode appelée pour vérifier la validité de l'authentification
    public boolean identification() throws MalformedURLException, IOException
    {
        URL url = new URL("http://"+properties.getProperty("adress")+":"+properties.getProperty("port")+"/login");
        HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
        connexion.setDoOutput(true);
        connexion.setRequestMethod("POST");
        
        try
        {
            String json = this.generateJson();
            OutputStreamWriter out = new OutputStreamWriter(connexion.getOutputStream());
            out.write(json);
            out.close();
            
            if (connexion.getResponseCode() == 200)
            {
                this.logged = true;
                return true;
            }
        }
        catch(Exception e)
        {}
        finally
        {
            System.out.println(connexion.getResponseCode());
            System.out.println(connexion.getResponseMessage());
        }
        
        // Erreur : Affichage d'un FacesMessage
        String failure = ResourceBundle.getBundle("/Langues/Bundle").getString("authenticationFailure");
        
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, failure, failure);
        context.addMessage(null, message);
        return false;
    }
    
    @Remove
    public void deconnecter()
    {
        this.logged = false;
        this.login = "";
        this.password = "";
    }
    
    public boolean isLogged()
    {
        return this.logged;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
      // Génération du JSON associé à l'User courant
    public String generateJson() throws JSONException
    {
        JSONObject json = new JSONObject();
        json.put("login", this.login);
        json.put("password", this.password);
        return json.toString();
    }
}
